package com.zhongyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhongyu.mapper.OrgMapper;
import com.zhongyu.pojo.Org;
import com.zhongyu.service.OrgService;
import com.zhongyu.vo.PageResult;
import com.zhongyu.vo.ResultVo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
@Service("orgService")
public class OrgServiceImpl extends ServiceImpl<OrgMapper, Org> implements OrgService {
    @Resource
    private OrgMapper orgMapper;
    @Override
    public PageResult  searchAll(Integer pageNo, Integer pageSize,Org org) {
        // QueryWrapper<User> queryWrapper=Wrappers.<User>query();
        LambdaQueryWrapper<Org> wrapper= Wrappers.<Org>lambdaQuery();

        wrapper.like(!StringUtils.isEmpty(org.getOrgName()),Org::getOrgName,org.getOrgName())
                .eq(org.getOrgTele()!=null,Org::getOrgTele,org.getOrgTele());
               /* .like(!StringUtils.isEmpty(org.getOrgTele()),Org::getOrgTele,org.getOrgTele());*/
        Page page=new Page(pageNo,pageSize);
        List<Org> list=orgMapper.findAll(page,wrapper);
       //System.out.println("==>"+list.size());
       /* return PageResult.getInstanceByPage(page);*/
        return new PageResult(list,page.getTotal());
    }

    @Override
    public List<Org> searchAll() {
        return orgMapper.selectList(null);
    }

    @Override
    public void add(Org org) {
        orgMapper.insert(org);
    }
    @Override
    public void update(Org org) {

        orgMapper.updateById(org);
    }

    @Override
    public void removeId(Integer id) throws ResultVo {
       Integer count= orgMapper.selectYuan(id);
       if(count==null){
           orgMapper.deleteById(id);
       }else{
           throw ResultVo.failure("该部门下面有员工，不能删除") ;
       }
    }
}
